import requests
import json
import time

course_list = [
    "前端开发简介",
            "表格案例实战",
            "表单案例实战",
            "CSS基础",
            "CSS进阶",
            "定位布局",
            "弹性盒子布局",
            "网格布局",
            "响应式布局",
            "移动端布局",
            "长页面布局",
            "JavaScript核心语法",
            "DOM",
            "BOM",
            "JavaScript高级进阶",
            "AJAX",
            "MySQL概述",
            "数据库和表",
            "数据查询",
            "数据更新与删除",
            "复杂查询",
            "常用函数",
            "触发器",
            "存储过程",
            "索引",
            "事务",
            "性能优化",
            "高可用MySQL",
            "MySQL8重要心特性",
            "用户与权限",
            "数据备份与恢复",
            "日志管理",
            "Java语言概述",
            "基本语法",
            "分支结构",
            "循环结构",
            "数组",
            "字符串",
            "面向对象（上）",
            "面向对象（下）",
            "集合",
            "异常",
            "I/O",
            "多线程",
            "网络编程",
            "反射",
            "注解和枚举",
            "JDK1.8新特性",
            "Linux操作系统入门",
            "Linux操作系统基础",
            "Vim文本编辑器",
            "用户与权限管理",
            "文件与目录管理",
            "磁盘管理与文件系统",
            "网络与防火墙管理",
            "进程与服务管理",
            "Linux实用指令",
            "JAVA应用-搭建部署环境",
            "阿里云及Ubuntu服务器简介",
            "大数据应用-Shell基础语法",
            "大数据应用Shell-高级编程",
            "云计算应用-Docker基础",
            "云计算应用-数据卷和网络",
            "云计算应用-DockerFile",
            "JavaScript基础",
            "流程控制语句",
            "函数",
            "对象",
            "数组与集合",
            "String对象与正则表达式",
            "JavaScript高级编程",
            "事件处理",
            "文档对象模型",
            "浏览器对象模型",
            "Style对象与Form对象",
            "JavaScript中的XML与JSON",
            "JavaScript的安全",
            "Ajax技术",
            "Vue.js编程",
            "项目实战-星光电影网",
            "项目构建工具Maven基础",
            "Web服务器-Tomcat",
            "HTTP协议详解",
            "Servlet技术",
            "Servlet API详解",
            "会话管理",
            "JSP技术",
            "EL和JSTL",
            "JavaBean技术与JPS开发模型",
            "过滤器",
            "监听器",
            "文件上传下载",
            "Ajax",
            "数据库连接池与DBUtils工具",
            "网上蛋糕商城（上）",
            "网上蛋糕商城（下）",
            "Hadoop介绍",
            "Hadoop集群的搭建及配置",
            "Hadoop基础操作",
            "Hadoop基础操作-运行任务",
            "MapReduce编程入门",
            "MapReduce进阶编程",
            "MapReduce高级编程",
            "Hive数据仓库基础操作",
            "Hive表的操作",
            "Hive表数据的增删改查",
            "HBase分布式数据库基础入门",
            "安装部署HBase集群",
            "使用HBase Java API",
            "项目实战-电影网站用户影评分析（一）",
            "项目实战-电影网站用户影评分析（二）",
            "项目实战-电影网站用户影评分析（三）"
]

with open ("Authorization.json", "r", encoding="utf-8") as f:
    Authorization = json.load(f)
    Token = Authorization['token']

url = "http://69.231.135.101/pgktstudent/studentCourse/queryCourseDetail"
headers = {
    "Accept": "application/json, text/plain, */*",
    "Content-Type": "application/json",
    "Authorization": Token
}

def fetch_course_details(course, max_retries=10, sleep_interval=2):
    payload = {"course": course}
    for attempt in range(1, max_retries+1):
        try:
            resp = requests.post(url, headers=headers, data=json.dumps(payload), verify=False, timeout=10)
            if resp.status_code == 200:
                data = resp.json()
                if data.get("code") == 200:
                    return data
                else:
                    print(f"课程《{course}》第{attempt}次尝试响应code不为200: {data.get('code')}")
            else:
                print(f"课程《{course}》第{attempt}次尝试http code={resp.status_code}")
        except Exception as e:
            print(f"课程《{course}》第{attempt}次尝试异常: {e}")
        time.sleep(sleep_interval)
    print(f"课程《{course}》多次尝试后仍未成功，跳过。")
    return None

results = {}

for course in course_list:
    data = fetch_course_details(course)
    course_versions = []
    if data and "courseList" in data.get("data", {}):
        for chapter in data["data"]["courseList"]:
            for child in chapter.get("children", []):
                course_versions.append({
                    "chapter": chapter.get("name"),
                    "version_name": child.get("versionName"),
                    "version_id": child.get("version_id"),
                    "outline_detail_id": child.get("outline_detail_id")
                })
    results[course] = course_versions
    print(f"课程《{course}》处理完毕，共提取{len(course_versions)}个章节。")

# 保存结果
with open("course_versions.json", "w", encoding="utf-8") as f:
    json.dump(results, f, ensure_ascii=False, indent=2)
print("全部完成，结果已保存到 course_versions.json")